var cfg = require('../../utils/cfg');
var socketUtil = require('../../utils/socketUtil');

var app = getApp();

Page({
  onShareAppMessage: function (res) {
    let that = this;
    let model = -1;

    if (res.from === 'button') {
      model = res.target.dataset.model;
      console.log(">>>>>model=" + model);
      console.log("from button");
    } else {
      console.log("from menu");
    }

    //设置分享page值
    if (res.from === 'menu') {
      that.data.sharePage = '/pages/home/home';
    } else if (model == 0) {
      that.data.sharePage = '/pages/readyOther/readyOther?roomTicket=' + wx.getStorageSync('roomTicket') + '&openId=' + wx.getStorageSync('loginUser').openId + '&masterAva=' + that.data.userInfo.avatarUrl;
    } else {

      that.data.sharePage = '/pages/moreReadyOther/moreReadyOther?roomTicket=' + wx.getStorageSync('roomTicket') + '&openId=' + wx.getStorageSync('loginUser').openId + '&masterAva=' + that.data.userInfo.avatarUrl;

    }

    console.log('>>>>>>分享地址' + that.data.sharePage);

    return {
      title: app.globalData.userInfo.nickName + '邀你一起调查问卷',
      path: that.data.sharePage,
      success: function (res) {
        
        var shareTickets = {};
        if (model == -1) {
          wx.reLaunch({
            url: '/pages/home/home'
          })
        } else if (model == 1) {
          if (res.shareTickets) {
            wx.reLaunch({
              url: '/pages/moreReady/moreReady?modelType=1'
            })
          } else {
            this.path = '/pages/home/home';
            that.getRoomTicket();
            wx.showToast({
              title: '请分享到微信群',
              icon: 'none',
              duration: 2000
            })
          }
          

        } else {
          //到双人模式去
          wx.reLaunch({
            url: '/pages/ready/ready?modelType=0'
          })
          return;
        }
        
      },
      fail: function (res) {

      }
    }
  },
  data: {
    hasQuestion: false,
    hasNewMsg: {
      question: false,
      ask: true
    },

    userInfo : {},

    modelType : 0,
    sharePage : '',

    shareParam : "",
    roomTicket : "",
    
    winnerdata: [],
    selfTop : {}
  },

  twoModel : function() {
    let that = this;

    that.data.modelType = 0;

    console.log('>>>>>>>双人模式');
  },

  moreModel: function () {
    this.data.modelType = 1;
    console.log('>>>>>>>多人模式');
  },

  setDisabled: function (e) {
    this.setData({
      disabled: !this.data.disabled
    })
  },
  setPlain: function (e) {
    this.setData({
      plain: !this.data.plain
    })
  },
  setLoading: function (e) {
    this.setData({
      loading: !this.data.loading
    })
  },
  onGotUserInfo: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    console.log(e.detail.rawData)
  },

  //页面加载
  onLoad: function(options) {
    console.log('>>>>>>>>>home onLoad()');
    this.getRoomTicket();

    //调用登录接口
    //app.wxlogin();

    var _self = this;
    _self.setData({
      userInfo: app.globalData.userInfo
    })

    wx.showShareMenu({
      withShareTicket: true,
      success : function(res) {
        console.log('share brefore>>>>>');
      },
      fail : function(res) {
        console.log('share brefore faild>>>>>');
      }
    })

    this.loadTop20();

  },

  loadTop20 : function() {
    if (!wx.getStorageSync('loginUser').openId) {
      return;
    }
    console.log(">>>>>>top20");
    let that = this;
    wx.request({
      url: cfg.host + cfg.mwurl.getTop20,
      data: {
        openId: wx.getStorageSync('loginUser').openId
      },
      success: function (res) {
        if (1000 !== res.data.code) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })

          return;
        }

        let topInfo = res.data.data;
        that.setData({ winnerdata: topInfo.top20, selfTop: topInfo.self});
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST'
    })
  },
  //渲染完成
  onReady: function() {
    console.log('>>>>>ehom onReady()');

    if (!wx.getStorageSync('userInfo')) {
      wx.redirectTo({
        url: '/pages/welcome/welcome?fromId=1000',
        success: function () {
          console.log('跳转到welcome页面');
        },
        fail: function () {
          console.log('跳转到welcome页面失败');
        }
      });
      return;
    }

  },
onShow : function() {
 console.log('>>>>>home onshow()');
  //注册回调函数
  socketUtil.registCallBack(this.doSocket);
},
doSocket : function(res) {
    console.log('home socket ' + res);
},

getRoomTicket : function() {
  var that = this;
  wx.request({
    url: cfg.host + '/share/getTicket',
    success : function(res) {
      that.data.roomTicket = res.data.data;
      wx.setStorageSync('roomTicket', res.data.data);
      console.log('roomTicket=' + that.data.roomTicket);

      
      

    }
  })
},

sendUserLogin : function() {
    wx.request({
      url: 'http://127.0.0.1:8088/user/hello',
      data : {
          username : 'tom',
          age : 90
      },
      method : "POST",
      header : {

      },
      success : function(res) {
        console.log(res.data);
      }
    })
  }

})